function [] = fig_analyze(X, Y, filenames, labelX, labelY, mag)
%FIG_ANALYZE Window for the display of rating statistics
%   License: https://darma.codeplex.com/license

    if isempty(X), return; end
    defaultBackground = get(0,'defaultUicontrolBackgroundColor');
    handles.figure_analyze = figure( ...
        'Name','DARMA: Analyze Ratings', ...
        'Units','pixels', ...
        'Position',[0 0 500 550], ...
        'NumberTitle','off', ...
        'MenuBar','none', ...
        'ToolBar','none', ...
        'Visible','off', ...
        'Resize','off', ...
        'Color',defaultBackground);
    movegui(handles.figure_analyze,'center');
    uicontrol('Style','text', ...
        'Parent',handles.figure_analyze, ...
        'Units','normalized', ...
        'Position',[.05 .925 .90 .04], ...
        'HorizontalAlignment','center', ...
        'FontSize',12, ...
        'String','Axis Configuration');
    handles.configuration = uitable(...
        'Parent',handles.figure_analyze, ...
        'Units','normalized', ...
        'Position',[.05 .800 .90 .10], ...
        'ColumnName',{'X-Axis Label','Y-Axis Label','Axis Magnitude'}, ...
        'ColumnWidth',{150,150,148.5}, ...
        'RowName',[], ...
        'Data',{labelX,labelY,mag}, ...
        'FontSize',11);
    jscrollpane = findjobj(handles.configuration);
    jTable = jscrollpane.getViewport.getView;
    cellStyle = jTable.getCellStyleAt(0,0);
    cellStyle.setHorizontalAlignment(cellStyle.CENTER);
    jTable.repaint;
    uicontrol('Style','text', ...
        'Parent',handles.figure_analyze, ...
        'Units','normalized', ...
        'Position',[.05 .72 .90 .04], ...
        'HorizontalAlignment','center', ...
        'FontSize',12, ...
        'String','Descriptive Statistics');
    handles.descriptives = uitable(...
        'Parent',handles.figure_analyze, ...
        'Units','normalized', ...
        'Position',[.05 .42 .90 .275], ...
        'ColumnName',{'Annotation File','X Mean','X SD','Y Mean','Y SD'}, ...
        'ColumnWidth',{143,72,72,72,72}, ...
        'RowName',[], ...
        'Data',[], ...
        'FontSize',10);
    set(findjobj(handles.descriptives),'VerticalScrollBarPolicy',javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    % Reliability Table
    uicontrol('Style','text', ...
        'Parent',handles.figure_analyze, ...
        'Units','normalized', ...
        'Position',[.05 .35 .90 .04], ...
        'HorizontalAlignment','center', ...
        'FontSize',12, ...
        'String','Inter-Rater Agreement and Reliability');
    handles.reliability = uitable(...
        'Parent',handles.figure_analyze, ...
        'Units','normalized', ...
        'Position',[.05 .05 .90 .275], ...
        'ColumnName',{'Index of Agreement/Reliability','X-Axis','Y-Axis'}, ...
        'ColumnWidth',{230,109,109}, ...
        'RowName',[], ...
        'Data',[], ...
        'FontSize',10);
    index = any(isnan(X),2);
    X2 = X;
    Y2 = Y;
    X2(index,:) = [];
    Y2(index,:) = [];
	r = size(X2,2);
    % Calculate and display descriptives
    desc = get(handles.descriptives,'Data');
    for i = 1:r
        desc{i,1} = filenames{i};
        desc{i,2} = num2str(nanmean(X(:,i)),'%.3f');
        desc{i,3} = num2str(nanstd(X(:,i)),'%.3f');
        desc{i,4} = num2str(nanmean(Y(:,i)),'%.3f');
        desc{i,5} = num2str(nanstd(Y(:,i)),'%.3f');
    end
    set(handles.descriptives,'Data',desc);
    % Calculate and display reliability
    if r == 1
        reli = [];
    elseif r > 1
        reli{1,1} = ' Single-Score Agreement ICC';
        reli{1,2} = num2str(ICC_A_1(X2),'%0.3f');
        reli{1,3} = num2str(ICC_A_1(Y2),'%0.3f');
        reli{2,1} = ' Single-Score Consistency ICC';
        reli{2,2} = num2str(ICC_C_1(X2),'%0.3f');
        reli{2,3} = num2str(ICC_C_1(Y2),'%0.3f');
        reli{3,1} = ' Average-Score Agreement ICC';
        reli{3,2} = num2str(ICC_A_k(X2),'%0.3f');
        reli{3,3} = num2str(ICC_A_k(Y2),'%0.3f');
        reli{4,1} = ' Average-Score Consistency ICC';
        reli{4,2} = num2str(ICC_C_k(X2),'%0.3f');
        reli{4,3} = num2str(ICC_C_k(Y2),'%0.3f');
        reli{5,1} = ' Mean of Rectangular RWG scores';
        reli{5,2} = num2str(RWG(round(X2),2*mag,'uniform'),'%0.3f');
        reli{5,3} = num2str(RWG(round(Y2),2*mag,'uniform'),'%0.3f');
        reli{6,1} = ' Mean of Triangular RWG scores';
        reli{6,2} = num2str(RWG(round(X2),2*mag,'triangular'),'%0.3f');
        reli{6,3} = num2str(RWG(round(Y2),2*mag,'triangular'),'%0.3f');
    else
        return;
    end
    set(handles.reliability,'Data',reli);
    set(handles.figure_analyze,'Visible','on');
end